package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/role")
@Slf4j
@CrossOrigin
public class RoleController {
    //1获取角色分页列表（带搜索）

    @Autowired
    private RoleService roleService;


    @GetMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            RoleQueryVo roleQueryVo) {
        Page<Role> pageParam = new Page<>(page,limit);
        IPage<Role> pageModel = roleService.selectPage(pageParam,roleQueryVo);

        return Result.ok(pageModel);

    }

//    save(role) {
//        return request({
//                url: `${api_name}/save`,
//        method: 'post',
//                data: role
//    })
//    },
    //2.保存角色
    @ApiOperation("保存角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role){
        roleService.save(role);
        return Result.ok(null);
    }

    //3.根据id查询角色

    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }


    //4.修改角色
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role role){
        roleService.updateById(role);
        return Result.ok(null);
    }


    // 6.删除某个角色
    @ApiOperation("删除某个角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        roleService.removeById(id);
        return Result.ok(null);
    }

    //7.批量删除多个角色
    @ApiOperation("批量删除多个角色")
    @DeleteMapping("batchRemove")
    public Result removeRoles(@RequestBody List<Long> ids){
        roleService.removeBatchByIds(ids);
        return Result.ok(null);
    }
}
